<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body style="height: 3000px">
  <div id="app">
    <button @click="isShow = !isShow">{{isShow? '隐藏': '显示'}}</button>
    <home v-if="isShow"></home>
  </div>
  <script src="./vue.js"></script>
  <script>
    const Home = {
      template: `
        <div>
          <h2>这是home组件</h2>
        </div>
      `,
      data(){
        return {
          timer: null
        }
      },
      mounted(){
        console.log('组件初始化dom渲染完成');
        this.timer = setInterval(() => {
          console.log(1);
        }, 1000);
        window.onscroll = () => {
          const sTop = document.documentElement.scrollTop || document.body.scrollTop;
          console.log(sTop);
        }
      },
      beforeDestroy(){
        clearInterval(this.timer);
        // 注销全局事件
        window.onscroll = null;
      }
    }
    Vue.component('Home', Home)
    // vm 全局只有一个 功能 将 vue语法绑定在#app
    const vm = new Vue({
      el: '#app',
      data: {
        isShow: true
      }
    })
  </script>
</body>
</html>